﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;
using Sector7.Medical.Consts;
using System.ComponentModel;
using Sector7.Medical.Utility;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;
using Sector7.Medical.INFM.Controls;

namespace Sector7.Medical.INFM.Windows
{
    /// <summary>
    /// AddNatureTypeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddNatureTypeWindow : AddModeWindowBase
    {
        #region Accessor
        public M7_DrugType NatureType
        {
            get { return m_entity as M7_DrugType; }
            set { m_entity = value; }
        }

        public M7_DrugType CurrentParentType
        {
            get { return m_currentParentType; }
            set
            {
                m_currentParentType = value;
                if (cbbParents.Items.Count > 0)
                {
                    cbbParents.SelectedItem = m_currentParentType;
                }
            }
        }
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        protected override void AddData()
        {
            switch (m_crud)
            {
                case CrudType.Create:
                    string parentId = NatureType.ParentId.ToString();
                    int seed = Agent_SYSS.GetBinarySeed(C_M7_TableName.DRUG_TYPE, parentId);

                    NatureType.DrugTypeId = Convert.ToInt32(PrimaryKeyUtil.GetPrimaryKey(C_M7_TableName.DRUG_TYPE, parentId, seed));

                    Agent_INFS.AddNatureType(NatureType);
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    var delta = GetDelta(C_ColumnName.DRUG_TYPE_ID, ref isChanged);
                    if (isChanged)
                    {
                        Agent_INFS.UpdateDrugType(delta);
                    }
                    break;
                default:
                    break;
            }

            base.AddData();
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            NatureType.DrugTypeName = txbName.Text;
            if (string.IsNullOrWhiteSpace(NatureType.DrugTypeName))
            {
                m_errors.Add("类型名称", "不能为空！");
            }
            if (cbbParents.SelectedItem == null)
            {
                m_errors.Add("所属类型", "不能为空！");
            }
            else
            {
                NatureType.ParentId = Convert.ToInt32(cbbParents.SelectedValue);
            }

            return base.CheckData();
        }

        protected override void InitializeForCreating()
        {
            ClientUtil.LoadItemsSource(cbbParents, (Menvironments.MainControl as DrugSummaryControl).GetPhysicalTypeList());

            NatureType = new M7_DrugType();
            NatureType.Flags = C_DrugTypeFlags.NATURE;

            txbName.Text = string.Empty;
            if (m_currentParentType != null)
            {
                cbbParents.SelectedItem = m_currentParentType;
            }

            base.InitializeForCreating();
        }

        protected override void InitializeForUpdating()
        {
            ClientUtil.LoadItemsSource(cbbParents, (Menvironments.MainControl as DrugSummaryControl).GetPhysicalTypeList());

            btnContinue.Visibility = Visibility.Hidden;
            cbbParents.SelectedValue = NatureType.ParentId;
            txbName.Text = NatureType.DrugTypeName;

            base.InitializeForUpdating();
        }
        #endregion

        #region Structure
        public AddNatureTypeWindow()
        {
            InitializeComponent();

            FocusedControl = txbName;
        }
        #endregion

        #region Inner members
        private M7_DrugType m_currentParentType = null;
        #endregion
    }
}
